﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.Framework.ProviderLoader.Configuration;
using PSE.ProcessManager.ResourceAccess.FileSystemProvider;
using PSE.ProcessManager.Entity;
using System.Windows.Forms;
using PSE.ProcessManager.ResourceAccess;
using System.ComponentModel;
using System.IO;

namespace ConfigurationManager
{
    public class Exporter
    {
        private IProcessManagerResourceAccess dataBaseResourceAccess;

        public Exporter()
        {
            dataBaseResourceAccess = ProcessManagerResourceAccessFactory.CreateProvider();
        }

        public void ExportAll(string outputPath)
        {
            if (!Directory.Exists(outputPath))
                Directory.CreateDirectory(outputPath);

            string inteligenceModuleFileName = outputPath + "\\" + "InteligenceModules.xml";
            string processFileName = outputPath + "\\" + "Processes.xml";
            string messageFileName = outputPath + "\\" + "Messages.xml";

            InteligenceModuleResourceAccess inteligenceModuleResourceAccess = new InteligenceModuleResourceAccess(inteligenceModuleFileName);
            ProcessResourceAccess processResourceAccess = new ProcessResourceAccess(processFileName);
            MessageResourceAccess messageResourceAccess = new MessageResourceAccess(messageFileName);

            List<ProcessEntity> processes = new List<ProcessEntity>();
            List<InteligenceModuleEntity> inteligenceModules = new List<InteligenceModuleEntity>();
            List<MessageEntity> messages = new List<MessageEntity>();

            processes = dataBaseResourceAccess.RetrieveProcessList();
            inteligenceModules = dataBaseResourceAccess.RetrieveInteligenceModules();
            messages = dataBaseResourceAccess.RetrieveMessageList(null);

            processResourceAccess.SaveToFile(processes);
            messageResourceAccess.SaveToFile(messages);
            inteligenceModuleResourceAccess.SaveToFile(inteligenceModules);
        }

        public void Export(List<ProcessEntity> processes, string outputPath)
        {
            if (!Directory.Exists(outputPath))
                Directory.CreateDirectory(outputPath);

            string inteligenceModuleFileName = outputPath + "\\" + "InteligenceModules.xml";
            string processFileName = outputPath + "\\" + "Processes.xml";
            string messageFileName = outputPath + "\\" + "Messages.xml";

            InteligenceModuleResourceAccess inteligenceModuleResourceAccess = new InteligenceModuleResourceAccess(inteligenceModuleFileName);
            ProcessResourceAccess processResourceAccess = new ProcessResourceAccess(processFileName);
            MessageResourceAccess messageResourceAccess = new MessageResourceAccess(messageFileName);

            List<InteligenceModuleEntity> inteligenceModules = new List<InteligenceModuleEntity>();
            List<MessageEntity> messages = new List<MessageEntity>();

            inteligenceModules = dataBaseResourceAccess.RetrieveInteligenceModules();

            foreach (ProcessEntity pe in processes)
            {
                messages.AddRange(dataBaseResourceAccess.RetrieveMessageList(pe.Id));
            }

            processResourceAccess.SaveToFile(processes);
            messageResourceAccess.SaveToFile(messages);
            inteligenceModuleResourceAccess.SaveToFile(inteligenceModules);
        }
    }
}
